﻿using ALEXBlog.Common.Attributes;
using ALEXBlog.IRepository.Base;
using ALEXBlog.IService.Base;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace ALEXBlog.Service.Base
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        public IBaseRepository repositoryContext;
        public BaseService(IBaseRepository repository)
        {
            repositoryContext = repository;
        }
        public virtual async Task<int> AddAsync(params TEntity[] models)
        {
            return await repositoryContext.AddAsync(models);
        }

        [CacheAttribute(AbsoluteExpiration = 40)]
        public virtual async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await repositoryContext.FindAsync(whereExpression);
        }
        [CacheAttribute(AbsoluteExpiration = 40)]
        public virtual async Task<List<TEntity>> QueryAsync()
        {
            return await repositoryContext.QueryAsync<TEntity>();
        }
        [CacheAttribute(AbsoluteExpiration = 40)]
        public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await repositoryContext.QueryAsync(whereExpression);
        }
        [CacheAttribute(AbsoluteExpiration = 40)]
        public virtual async Task<List<TEntity>> QueryAsync(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> whereExpression)
        {
            return await repositoryContext.QueryAsync(pageIndex, pageSize, whereExpression);
        }

        public virtual async Task<int> RemoveAsync(params TEntity[] model)
        {
            return await repositoryContext.RemoveAsync(model);
        }
        public virtual async Task<int> RemoveAsync(object id)
        {
            return await repositoryContext.RemoveAsync<TEntity>(id: id);
        }
        public async Task<int> RemoveAsync(params object[] ids)
        {
            return await repositoryContext.RemoveAsync<TEntity>(ids: ids);
        }

        public async Task<int> UpdateAsync(params TEntity[] models)
        {
            return await repositoryContext.UpdateAsync(models);
        }
    }
}
